package cn.wangzg.course0802;

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private MyHelper helper;
    private TextView tvSqlite;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        helper = new MyHelper(this);
        tvSqlite = findViewById(R.id.tv_sqlite);

        AppDatabase db = Room.databaseBuilder(getApplicationContext(),
                AppDatabase.class, "dbroom.db")
                .allowMainThreadQueries().build();

        User user1 = new User();
        user1.firstName = "aaa";
        user1.lastName = "bbb";
        user1.uid = 3;

        User user2 = new User();
        user2.firstName = "ccc";
        user2.lastName = "ddd";
        user2.uid = 2;
        db.userDao().insertAll(user1, user2);

        List<User> users = db.userDao().getAll();
        for (User u : users) {

            System.out.println(u.firstName + "---" + u.lastName);
        }
    }

    public void btnInsert(View view) {
        SQLiteDatabase db = helper.getWritableDatabase();
        //ContentValues等同于Map
        ContentValues values = new ContentValues();
        values.put("name", "aaaa");//key：必须为数据表的列名
        values.put("age", 23);
        //nullColumnHack：插入时需要忽略的列
        db.insert("person", null, values);
    }

    public void btnUpdate(View view) {
        SQLiteDatabase db = helper.getWritableDatabase();

        ContentValues values = new ContentValues();
        //values.put("name","aaaa");
        values.put("age", 25);
        /**
         * update table
         * set xxx=?,xxx=?      --values
         * where xxx=?
         */
        db.update("person", values, "name=?", new String[]{"aaaa"});
    }

    public void btnDelete(View view) {
        SQLiteDatabase db = helper.getWritableDatabase();
        /**
         * delete table
         * where xxx=?
         */
        db.delete("person", "age<?", new String[]{"30"});
    }

    public void btnQuery(View view) {
        SQLiteDatabase db = helper.getReadableDatabase();
        /**
         * select *
         * from table
         * where name=? and age=?
         * group by
         * having
         * order
         */
        Cursor cursor = db.query("person", null, null, null,
                null, null, null);
        if (cursor == null) return;
        int nameIndex = cursor.getColumnIndex("name");
        int ageIndex = cursor.getColumnIndex("age");
        while (cursor.moveToNext()) {
            String name = cursor.getString(nameIndex);
            int age = cursor.getInt(ageIndex);
            tvSqlite.setText(name + "   " + age);
            //System.out.println(name);
        }
        cursor.close();
    }
}
